package com.cq.controller;

import com.cq.pojo.Admin;
import com.cq.pojo.Result;
import com.cq.pojo.Role;
import com.cq.service.AdminService;
import com.cq.service.RoleService;
import org.apache.ibatis.annotations.Delete;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 悠久的苍穹
 * @Date: 2021/1/19 20:24
 * @Description: 角色控制层
 * @Version: 1.0
 */
@Controller
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    AdminService adminService;

    @ResponseBody
    @GetMapping("/role/selectRoleList")
    public List<Role> selectRoleList() {
        List<Role> list = roleService.getRoleList();
        return list;
    }

    @ResponseBody
    @GetMapping("/role/getAllRoles")
    public Result getAllRoles() {
        Result result = new Result();
        List<Role> list = roleService.getRoleList();
        result.setCode(0);
        result.setData(list);
        return result;
    }
    @GetMapping("/editRoleUI/{id}")
    public String editRoleUI(@PathVariable("id") int id, Model model){
        Role role = roleService.findRoleById(id);
        model.addAttribute("rol",role);
        return "user/administrators/roleform";
    }

    @ResponseBody
    @PutMapping("/role/updateRole")
    public Result updateRole(@RequestBody Map<String,Object> map){
        Result result = new Result();
        Integer id = Integer.parseInt(map.get("id").toString());
        String name = map.get("name").toString();
        String desc = map.get("desc").toString();
        Role role = new Role();
        role.setId(id);
        role.setName(name);
        role.setDesc(desc);
        int i = roleService.updateRole(role);
        if(i==1){
            result.setCode(200);

        }else{
            result.setCode(500);
        }
        return result;
    }

    @ResponseBody
    @PutMapping("/addRole")
    public Result addRole(@RequestBody Map<String,Object> map){
        Result result = new Result();
        String name = map.get("name").toString();
        String desc = map.get("desc").toString();
        Role role = new Role();
        role.setName(name);
        role.setDesc(desc);
        int i = roleService.addRole(role);
        if(i==1){
            result.setCode(200);

        }else{
            result.setCode(500);
        }
        return result;
    }

    @ResponseBody
    @DeleteMapping("/delRole/{id}")
    public Result deleteRole(@PathVariable("id") String id){
        Result result = new Result();
        result.setCode(200);
        String[] s = id.split(",");
        for (int i = 0; i < s.length; i++) {
            Integer id2 = Integer.parseInt(s[i]);
            Admin admin = adminService.findAdminByRoleId(id2);
            if(admin==null){
                roleService.deleteRole(id2);
            }else{
                result.setCode(500);
                break;
            }
        }
        return result;
    }

    @GetMapping(value = "/getPermission/{id}")
    public String getMenu(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("id",id);
        return "/user/administrators/permission";
    }
}
